package com.ruoyi.home.controller;

import java.io.ByteArrayInputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import afu.org.checkerframework.checker.oigj.qual.O;
import com.biyuhan.minioFile.service.MinioService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.home.domain.*;
import com.ruoyi.home.service.ITSfContractService;
import com.ruoyi.home.utils.BeanUtils;
import com.ruoyi.home.utils.HgFileUtils;
import com.ruoyi.home.utils.PayMentPlanGenerator;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;


/**
 * 收房Controller
 * 
 * @author weishu
 * @date 2021-07-21
 */
@RestController
@RequestMapping("/contract")
@CrossOrigin
public class TSfContractController extends BaseController
{
    @Autowired
    private ITSfContractService tSfContractService;


    @Autowired
    private MinioService minioService;

    /**
     * 收房列表
     */
    @PreAuthorize(hasPermi = "system:contract:list")
    @GetMapping("/list")
    public TableDataInfo list(TSfContract tSfContract)
    {
        startPage();
        List<TSfContract> list = tSfContractService.selectTSfContractList(tSfContract);
        return getDataTable(list);
    }

    /**
     * 收房列表
     */
    @PreAuthorize(hasPermi = "system:contract:list")
    @GetMapping("/owner")
    public TableDataInfo owner(TSfContract tSfContract)
    {
        startPage();
        List<TSfContract> list = tSfContractService.selectTSfContractOwner(tSfContract);
        return getDataTable(list);
    }

    /**
     * 选择房产列表查询
     */
    @PreAuthorize(hasPermi = "system:contract:list")
    @GetMapping("/xzfc")
    public TableDataInfo xzfc(TSHouseInfoDto tsHouseInfoDto)
    {
        startPage();
       List<TSHouseInfoDto> list = tSfContractService.selectTSfContractXzfc(tsHouseInfoDto);
        return getDataTable(list);
    }



    /**
     * 选择银行列表查询
     */
    @PreAuthorize(hasPermi = "system:contract:list")
    @GetMapping("/bank")
    public TableDataInfo bank(TBankDto tBankDto)
    {
        startPage();
        List<TBankDto> list = tSfContractService.selectTSfContractBank(tBankDto);
        return getDataTable(list);
    }

    /**
     * 选择省级列表查询
     */
    @PreAuthorize(hasPermi = "system:contract:list")
    @GetMapping("/region")
    public TableDataInfo region(TRegionDto tRegionDto)
    {
        startPage();
        List<TRegionDto> list = tSfContractService.selectTSfContractRegion(1);
        return getDataTable(list);
    }

    /**
     * 选择图片添加
     */
   // @PreAuthorize(hasPermi = "system:contract:list")
    @RequestMapping("/addpic")
    public String addpic(MultipartFile file)
    {
        String s = minioService.updateFile(file);
        Map<String, Object> pic = new HashMap<>();
        pic.put("picurl",s);
        Date date = new Date();
        pic.put("date",date);
        tSfContractService.addpic(pic);
        return s;
    }

    @PreAuthorize(hasPermi = "system:contract:list")
    @GetMapping("/scfkjh")
    private Object generatePayMentPlan(TSfContract tSfContract) throws ParseException {
        // 计算生成各账期付款计划
        /*创建实体*/
        CalculateFkPlanForm dataPlan = new CalculateFkPlanForm();
        /*赋值*/
        BeanUtils.copy(tSfContract,dataPlan);
        dataPlan.setDepositType(Long.hashCode(tSfContract.getDepositType()));/*押*/
        dataPlan.setPaymentType(Long.hashCode(tSfContract.getPaymentType()));/*付*/
        dataPlan.setMonthRent(tSfContract.getMonthRent());// 收房月租金
        dataPlan.setDepositMoney(tSfContract.getDepositMoney());// 房产押金
        System.out.println("-------->"+dataPlan);
        /*生成付款表单*/
        List<CwPaymentPlanDto> payMentPlanList = PayMentPlanGenerator.generatePayMentPlan(dataPlan);
        /*循环赋值付款状态*/
        for (CwPaymentPlanDto pa: payMentPlanList) {
            pa.setSkStatus(1);
        }
        /*创建返回map*/
        Map<String,Object> mv = new HashMap<>();
        /*添加生成付款表单*/
        mv.put("payMentPlanList", payMentPlanList);
        /*押金*/
        BigDecimal totalDeposit = tSfContract.getDepositMoney()
                .multiply(new BigDecimal(tSfContract.getDepositType()));
        if (BigDecimal.ZERO.equals(totalDeposit)) {
            // 无押金
            mv.put("hasDeposit", 0);
        } else {
            // 有押金
            mv.put("hasDeposit", 1);
        }
        if (null != tSfContract.getLastContractId()) {
            mv.put("isXuYue", 1);// 续约(续约时自动生成的是已付的付款单，支付状态不可编辑)
        } else {
            mv.put("isXuYue", 0);
        }
        // 合同总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (CwPaymentPlanDto cwPaymentPlanDto : payMentPlanList) {
            totalAmount = totalAmount.add(cwPaymentPlanDto.getPaymentAmount());
        }

        mv.put("totalAmount", totalAmount);
        mv.put("preCycleChange", 0);
        return mv;
    }

    /**
     * 保存数据
     */
    @PreAuthorize(hasPermi = "system:contract:list")
    @GetMapping("/baocun")
    public AjaxResult baocun(TSfContract tSfContract)
    {
        /*添加业主*/
        if (tSfContract!=null){
            int iOwner= tSfContractService.addOwnerService(tSfContract);
            if (iOwner==0){
                return AjaxResult.error("数据添加失败失败");
            }
        }
        /*添加合同*/
        if (tSfContract!=null){
            int iContract=tSfContractService.insertTSfContract(tSfContract);
            if (iContract==0){
                return AjaxResult.error("数据添加失败失败");
            }
        }
        return toAjax(200);

    }

}
